﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Inovance.InoMotionCotrollerShop.InoServiceContract.EtherCATConfigApi;
namespace example
{
    public partial class Form1 : Form
    {
        public List<int> mCaptPosList;
        public static UInt64 cardHandle;//轴卡操作句柄
        UInt32 retRtn; //函数接口返回值
        Int16 axNo = 0;//起始轴号
        Int16 count = 1;//轴数
        Int16 stopType = 1;//停止类型 0： 平滑停止 1： 急速停止

        public Form1()
        {
            InitializeComponent();
        }

        #region 初始化
        //打印日志
        private void ShowMessage(string msg)
        {
            listBox_RunSts.Items.Add(DateTime.Now.ToString() + " " + msg);
            listBox_RunSts.SelectedIndex = listBox_RunSts.Items.Count - 1;
            listBox_RunSts.SelectedIndex = -1;
        }        
        //卡初始化
        private void button_CardInit_Click(object sender, EventArgs e)
        {
            UInt32 masterStatus = 0;
            int cardNo = 0; //打开0号卡
            Int32 cardsNum = 0;
            try
            {
                //非必须步骤，读取电脑中轴卡的数量
                retRtn = ImcApi.IMC_GetCardsNum(ref cardsNum);
                if (retRtn != ImcApi.EXE_SUCCESS)
                {
                    ShowMessage("IMC_GetCardsNum failed 故障代码为：0x" + retRtn.ToString("x8"));
                }
                textBox_CardNum.Text = cardsNum.ToString();
                //【1】获取轴卡操作句柄
                retRtn = ImcApi.IMC_OpenCardHandle(cardNo, ref cardHandle);
                if (retRtn != ImcApi.EXE_SUCCESS)
                {
                    ShowMessage("IMC_OpenCardHandle failed 故障代码为：0x" + retRtn.ToString("x8"));
                }
                //【2】获取主站状态
                retRtn = ImcApi.IMC_GetECATMasterSts(cardHandle, ref masterStatus);
                if (retRtn != ImcApi.EXE_SUCCESS)
                {
                    ShowMessage("IMC_GetECATMasterSts failed 故障代码为：0x" + retRtn.ToString("x8"));
                }
                else
                {
                    if (masterStatus != ImcApi.EC_MASTER_OP)
                    {
                        //【3】下载设备参数
                        //deviceconfig.xml文件是由控制卡后台软件配置生成然后手动放到编译路径下，路径可自由选择；
                        retRtn = ImcApi.IMC_DownLoadDeviceConfig(cardHandle, ".\\deviceconfig.xml");
                        if (retRtn != ImcApi.EXE_SUCCESS)
                        {
                            ShowMessage("IMC_DownLoadDeviceConfig failed 故障代码为：0x" + retRtn.ToString("x8"));
                        }
                        //【4】启动总线
                        retRtn = ImcApi.IMC_ScanCardECAT(cardHandle, 1);            //0表示非阻塞式开卡，1表示阻塞式开卡；
                        if (retRtn != ImcApi.EXE_SUCCESS)
                        {
                            ShowMessage("IMC_ScanCardECAT failed 故障代码为：0x" + retRtn.ToString("x8"));
                        }
                        //【5】下载系统参数
                        //systemconfig.xml文件是由控制卡后台软件配置生成然后手动放到编译路径下，路径可自由选择；
                        retRtn = ImcApi.IMC_DownLoadSystemConfig(cardHandle, ".\\systemconfig.xml");
                        if (retRtn != ImcApi.EXE_SUCCESS)
                        {
                            ShowMessage("IMC_DownLoadSystemConfig failed 故障代码为：0x" + retRtn.ToString("x8"));
                        }
                    }
                }
                timer_UI.Enabled = true;
                textBox_CardNum.Text = cardsNum.ToString();
            }
            catch (Exception ex)
            {
                ShowMessage(ex.ToString());
            }
        }
        //关闭卡
        private void button_CloseCard_Click(object sender, EventArgs e)
        {
            try
            {
                timer_UI.Enabled = false;
                retRtn = ImcApi.IMC_CloseCard(cardHandle);
                if (retRtn != ImcApi.EXE_SUCCESS)
                {
                    ShowMessage(System.Reflection.MethodBase.GetCurrentMethod().Name + " failed 故障代码为：0x" + retRtn.ToString("x8"));
                    return;
                }
            }
            catch (Exception ex)
            {
                ShowMessage(ex.ToString());
            }
        }
        #endregion

        #region 控制
        //使能
        private void button_AxServoOn_Click(object sender, EventArgs e)
        {
            try
            {
                retRtn = ImcApi.IMC_AxServoOn(cardHandle, axNo, count);
                if (retRtn != ImcApi.EXE_SUCCESS)
                {
                    ShowMessage(System.Reflection.MethodBase.GetCurrentMethod().Name + " failed 故障代码为：0x" + retRtn.ToString("x8"));
                    return;
                }
                else
                {
                    ShowMessage("轴上使能成功");
                }
            }
            catch (Exception ex)
            {
                ShowMessage(ex.ToString());
            }
        }
        //下使能
        private void button_AxServoOff_Click(object sender, EventArgs e)
        {
            try
            {
                retRtn = ImcApi.IMC_AxServoOff(cardHandle, axNo, count);
                if (retRtn != ImcApi.EXE_SUCCESS)
                {
                    ShowMessage(System.Reflection.MethodBase.GetCurrentMethod().Name + " failed 故障代码为：0x" + retRtn.ToString("x8"));
                    return;
                }
                else
                {
                    ShowMessage("轴上失能成功");
                }
            }
            catch (Exception ex)
            {
                ShowMessage(ex.ToString());
            }
        }
        //停止
        private void button_AxMoveStop_Click(object sender, EventArgs e)
        {
            try
            {
                stopType = 1;//停止类型 0： 平滑停止 1： 急速停止
                retRtn = ImcApi.IMC_AxMoveStop(cardHandle, axNo, stopType);// 0平滑停止；1 急停停止
                if (retRtn != ImcApi.EXE_SUCCESS)
                {
                    ShowMessage(System.Reflection.MethodBase.GetCurrentMethod().Name + " failed 故障代码为：0x" + retRtn.ToString("x8"));
                    return;
                }
            }
            catch (Exception ex)
            {
                ShowMessage(ex.ToString());
            }
        }
        //清除轴状态
        private void button_ClrAxSts_Click(object sender, EventArgs e)
        {
            try
            {
                for (Int16 i = axNo; i < count; i++)
                {
                    retRtn = ImcApi.IMC_ClrAxSts(cardHandle, i);
                }
                if (retRtn != ImcApi.EXE_SUCCESS)
                {
                    ShowMessage(System.Reflection.MethodBase.GetCurrentMethod().Name + " failed 故障代码为：0x" + retRtn.ToString("x8"));
                    return;
                }
            }
            catch (Exception ex)
            {
                ShowMessage(ex.ToString());
            }
        }
        #endregion

        #region 轴状态与信息显示
        //将十进制整形转换为二进制显示的字符串
        private string ConvertDecimalToBinary(Int32 nAxSts) 
        {
            string strAxSts = "";
            Int32 nBitValue = 0x00;

            for (uint i = 0; i < 11; i++)
            {
                nBitValue = (Int32)(0x01 * Math.Pow(2.0, i));    //对nBitValue向左移1位，移到第几位通过i来控制
                if ((nAxSts & nBitValue) == nBitValue)
                {
                    strAxSts = "1" + strAxSts;
                }
                else
                {
                    strAxSts = "0" + strAxSts;
                }
            }
            return strAxSts;
        }
        //UI界面信息刷新
        private void timer_UI_Tick(object sender, EventArgs e)
        {
            try
            {
                timer_UI.Enabled = false;
                double[] PrfPosArray = { 0 };
                double[] EncPosArray = { 0 };
                double[] EncVelArray = { 0 };
                Int32[] AxStsArray = { 0 };
                Int16 CtrlMode = 0;
                Int16[] Status = { 0 };
                //Int16 dir = 0;//1 表示正向补偿表 -1 表示负向补偿表
                Int16 araNum = 0;//补偿点个数
                Int32[] CompValArray = {0,0,0,0,0,0};//补偿数据表
                Int16 Sts = 0;//获取螺距是否启动的状态
                Int32 CompVal = 0;//获取螺距误差补偿值
                string tmp = string.Empty;

                //获取目标位置
                retRtn = ImcApi.IMC_GetAxPrfPos(cardHandle, axNo, PrfPosArray, count);
                if (retRtn != ImcApi.EXE_SUCCESS)
                {
                    ShowMessage("IMC_GetAxSts failed 故障代码为：0x" + retRtn.ToString("x8"));
                    return;
                }
                //获取反馈位置
                retRtn = ImcApi.IMC_GetAxEncPos(cardHandle, axNo, EncPosArray, count);
                if (retRtn != ImcApi.EXE_SUCCESS)
                {
                    ShowMessage("IMC_GetAxEncPos failed 故障代码为：0x" + retRtn.ToString("x8"));
                    return;
                }
                //获取反馈速度
                retRtn = ImcApi.IMC_GetAxEncVel(cardHandle, axNo, EncVelArray, count);
                if (retRtn != ImcApi.EXE_SUCCESS)
                {
                    ShowMessage("IMC_GetAxSts failed 故障代码为：0x" + retRtn.ToString("x8"));
                    return;
                }
                //获取伺服状态
                retRtn = ImcApi.IMC_GetAxSts(cardHandle, axNo, AxStsArray, count);
                if (retRtn != ImcApi.EXE_SUCCESS)
                {
                    ShowMessage("IMC_GetAxSts failed 故障代码为：0x" + retRtn.ToString("x8"));
                    return;
                }
                //获取伺服控制模式
                retRtn = ImcApi.IMC_GetAxCtrlMode(cardHandle, axNo, ref CtrlMode);
                if (retRtn != ImcApi.EXE_SUCCESS)
                {
                    ShowMessage("IMC_GetAxCtrlMode failed 故障代码为：0x" + retRtn.ToString("x8"));
                }
                ////获取螺距误差补偿表
                //retRtn = ImcApi.IMC_AxGetScrewCompTable(cardHandle, axNo, dir ,ref araNum, CompValArray);
                //if (retRtn != ImcApi.EXE_SUCCESS)
                //{
                //    ShowMessage("IMC_AxGetScrewCompTable failed 故障代码为：0x" + retRtn.ToString("x8"));
                //    return;
                //}
                ////获取螺距是否启动的状态
                //retRtn = ImcApi.IMC_AxGetScrewCompStatus(cardHandle,axNo,ref Sts);
                //if (retRtn != ImcApi.EXE_SUCCESS)
                //{
                //    ShowMessage("IMC_AxGetScrewCompTable failed 故障代码为：0x" + retRtn.ToString("x8"));
                //    return;
                //}
                ////获取螺距误差补偿值
                ////public static extern UInt32 IMC_AxGetScrewCompValue(HANDLETYPE cardHandle,Int16 axNo,ref Int32 pCompVal);
                //retRtn = ImcApi.IMC_AxGetScrewCompValue(cardHandle,axNo,ref CompVal);
                //if (retRtn != ImcApi.EXE_SUCCESS)
                //{
                //    ShowMessage("IMC_AxGetScrewCompTable failed 故障代码为：0x" + retRtn.ToString("x8"));
                //    return;
                //}
                //X轴状态显示
                textBox_AxPrfPos0.Text = PrfPosArray[0].ToString();
                textBox_AxEncPos0.Text = EncPosArray[0].ToString();
                textBox_AxEncVel0.Text = EncVelArray[0].ToString();
                //转化为二进制字符串显示
                textBox_AxSts0.Text = ConvertDecimalToBinary(AxStsArray[0]);              
                //到位信号判断
                textBox_AxStsArrive0.Text = (AxStsArray[0] & 0xC) == 0x8 ? "1" : "0";
                textBox_CtrlMode.Text = CtrlMode.ToString();
                //螺距补偿监控参数                
                textBox_ScrewCompTableDir.Text = araNum.ToString();
                textBox_ScrewCompTableNum.Text = araNum.ToString();
                textBox_ScrewCompStatus.Text = Sts.ToString();
                textBox_ScrewCompValue.Text = CompVal.ToString();
                //显示数据补偿表
                for (int i = 0; i < CompValArray.Length; i++)
                {
                    tmp = tmp + CompValArray[i] + " ";
                }
                listBox_CompValArray.Items.Add(tmp);

                timer_UI.Enabled = true;
            }
            catch (Exception ex)
            {
                ShowMessage(ex.ToString());
            }            
        }
        #endregion    
        
        #region 功能测试        
        //设置螺距误差表
        private void button_AxSetScrewCompTable_Click(object sender, EventArgs e)
        {
            //try
            //{
            //    Int16 dir = 0;//1 表示正向补偿表 -1 表示负向补偿表
            //    Int32[] CompValArrayPos = {0,100,-50,60,80,30};//正向补偿数据表
            //    Int32[] CompValArrayNeg = {0,50 ,-30,70,60,-20};//负向补偿数据表
            //    //Int16 araNum = 6;

            //    dir = 1;
            //    retRtn = ImcApi.IMC_AxSetScrewCompTable(cardHandle, axNo, dir, araNum, CompValArrayPos);
            //    if (retRtn != ImcApi.EXE_SUCCESS)
            //    {
            //        ShowMessage("IMC_AxSetScrewCompTable failed 故障代码为：0x" + retRtn.ToString("x8"));
            //        return;
            //    }
            //    else
            //    {
            //        ShowMessage("IMC_AxSetScrewCompTable 成功");
            //    }

            //    dir = 0;
            //    retRtn = ImcApi.IMC_AxSetScrewCompTable(cardHandle, axNo, dir, araNum, CompValArrayNeg);
            //    if (retRtn != ImcApi.EXE_SUCCESS)
            //    {
            //        ShowMessage("IMC_AxSetScrewCompTable failed 故障代码为：0x" + retRtn.ToString("x8"));
            //        return;
            //    }
            //    else
            //    {
            //        ShowMessage("IMC_AxSetScrewCompTable 成功");
            //    }               
            //}
            //catch (Exception ex)
            //{
            //    ShowMessage(ex.ToString());
            //}
        }        
        #endregion
        //使能螺距误差补偿
        private void button_AxEnableScrewComp_Click(object sender, EventArgs e)
        {            
            //try
            //{
            //    Int32 posStartPos = 0;//起始补偿位置
            //    Int32 posEndPos = 100;//终止补偿位置
            //    Int16 pointNum = 0;//补偿点个数， 包括起末位置点
            //    retRtn = ImcApi.IMC_AxEnableScrewComp(cardHandle,axNo, posStartPos, posEndPos ,pointNum);
            //    if (retRtn != ImcApi.EXE_SUCCESS)
            //    {
            //        ShowMessage(System.Reflection.MethodBase.GetCurrentMethod().Name + " failed 故障代码为：0x" + retRtn.ToString("x8"));
            //    }
            //    else
            //    {
            //        ShowMessage(System.Reflection.MethodBase.GetCurrentMethod().Name + " 成功");
            //    }
            //}
            //catch (Exception ex)
            //{
            //    ShowMessage(ex.ToString());
            //}
        }
        //去除螺距补偿功能
        private void button_AxDisableScrewComp_Click(object sender, EventArgs e)
        {
            //try
            //{
            //    retRtn = ImcApi.IMC_AxDisableScrewComp(cardHandle,axNo);
            //    if (retRtn != ImcApi.EXE_SUCCESS)
            //    {
            //        ShowMessage(System.Reflection.MethodBase.GetCurrentMethod().Name + " failed 故障代码为：0x" + retRtn.ToString("x8"));
            //    }
            //    else
            //    {
            //        ShowMessage(System.Reflection.MethodBase.GetCurrentMethod().Name + " 成功");
            //    }
            //}
            //catch (Exception ex)
            //{
            //    ShowMessage(ex.ToString());
            //}
        }
    }
}
